package com.ruoyi.web.controller.hh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.HhProduct;
import com.ruoyi.domain.HhType;
import com.ruoyi.dto.SaleProductDto;
import com.ruoyi.service.IHhProductService;
import com.ruoyi.utils.ParamsDictUtil;
import com.ruoyi.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 商品信息Controller
 * @author chongjin
 * @date 2020-11-19
 */
@Controller
public class HhProductController extends BaseController {
    private String prefix = "hh/baseInfo";

    @Autowired
    private IHhProductService hhProductService;

    @GetMapping("/baseInfo/product")
    public String product(ModelMap mmap) {
        HhType hhType = new HhType();
        hhType.setParentId(Long.parseLong("0"));
        Map resultMap = hhProductService.selectProductInit(hhType);

        mmap.put("parentType", resultMap.get("typeList"));
        mmap.put("brandList", resultMap.get("brandList"));
        mmap.put("sizeList", resultMap.get("sizeList"));
        return prefix + "/product_manage";
    }

    /**
     * 查询商品类型
     */
    @PostMapping("/baseInfo/product/parentTypeChange")
    @ResponseBody
    public AjaxResult parentTypeChange(HhType hhType) {
        Map data = hhProductService.selectProductInit(hhType);
        return AjaxResult.success(data);
    }

    /**
     * 查询商品信息列表
     */
    @PostMapping("/baseInfo/product/list")
    @ResponseBody
    public TableDataInfo list(HhProduct hhProduct) {
        startPage();
        List<HhProduct> list = hhProductService.selectHhProductList(hhProduct);
        return getDataTable(list);
    }

    /**
     * 查询已售商品信息列表
     */
    @PostMapping("/baseInfo/product/productList")
    @ResponseBody
    public TableDataInfo productList(SaleProductDto saleProductDto) {
        startPage();
        List<HhProduct> list = hhProductService.selectSaleHhProductList(saleProductDto);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @Log(title = "导出商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/baseInfo/product/export")
    @ResponseBody
    public AjaxResult export(HhProduct hhProduct) {
        List<HhProduct> list = hhProductService.selectHhProductList(hhProduct);
        ExcelUtil<HhProduct> util = new ExcelUtil<HhProduct>(HhProduct.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 获取新增商品页面
     */
    @GetMapping("/baseInfo/product/add")
    public String add(ModelMap mmap) {
        HhType hhType = new HhType();
        hhType.setParentId(Long.parseLong("0"));
        Map resultMap = hhProductService.selectProductInit(hhType);

        mmap.put("parentType", resultMap.get("typeList"));
        mmap.put("brandList", resultMap.get("brandList"));
        mmap.put("sizeList", resultMap.get("sizeList"));
        return prefix + "/product_add";
    }

    /**
     * 新增商品保存
     */
    @Log(title = "新增商品", businessType = BusinessType.INSERT)
    @PostMapping("/baseInfo/product/add")
    @ResponseBody
    public AjaxResult addSave(HhProduct hhProduct) {
        return toAjax(hhProductService.insertHhProduct(hhProduct));
    }

    /**
     * 获取修改商品信息页面
     */
    @GetMapping("/baseInfo/product/edit/{productId}")
    public String edit(@PathVariable("productId") Long productId, ModelMap mmap) {
        HhProduct hhProduct = hhProductService.selectHhProductById(productId);
        mmap.put("hhProduct", hhProduct);
        return prefix + "/product_edit";
    }

    /**
     * 修改保存商品信息
     */
    @Log(title = "修改商品", businessType = BusinessType.UPDATE)
    @PostMapping("/baseInfo/product/edit")
    @ResponseBody
    public AjaxResult editSave(HhProduct hhProduct) {
        return toAjax(hhProductService.updateHhProduct(hhProduct));
    }

    /**
     * 删除商品信息
     */
    @Log(title = "删除商品", businessType = BusinessType.DELETE)
    @PostMapping("/baseInfo/product/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hhProductService.deleteHhProductByIds(ids));
    }

    /**
     * 下载模板
     */
    @GetMapping("/baseInfo/product/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<HhProduct> util = new ExcelUtil<HhProduct>(HhProduct.class);
        return util.importTemplateExcel("商品数据");
    }

    /**
     * 上传图片
     */
    @Log(title = "上传图片", businessType = BusinessType.UPDATE)
    @PostMapping("/baseInfo/product/uploadImage")
    @ResponseBody
    public AjaxResult uploadImage(@RequestParam("imageFile") MultipartFile imageFile) throws Exception {
        String fileName = FileUploadUtils.upload(ParamsDictUtil.PRODUCT_UPLOAD_PATH, imageFile);
        return AjaxResult.success().put("productImg", fileName);
    }

    /**
     * 导入数据
     */
    @PostMapping("/baseInfo/product/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<HhProduct> util = new ExcelUtil<HhProduct>(HhProduct.class);
        List<HhProduct> productListList = util.importExcel(file.getInputStream());
        String message = hhProductService.importProduct(productListList, updateSupport);
        return AjaxResult.success(message);
    }

}
